//
//  BTESelectAlertViewController.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/18.
//

import UIKit


class BTEListItemCell: UITableViewCell {

    let subTile = UILabel()
    
    let border = UIView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = .clear
        selectionStyle = .none
        contentView.backgroundColor = .clear
        
        border.isHidden = true
        contentView.addSubview(border)
        border.backgroundColor = .white
        border.roundCorners(.allCorners, radius: 16)
        border.layer.borderColor = UIColor.colorWithHex("#082252")!.cgColor
        border.layer.borderWidth = 3
        border.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.right.equalToSuperview().inset(28)
        }
        
        subTile.textAlignment = .center
        subTile.font = .systemFont(ofSize: 14, weight: .bold)
        subTile.textColor = .black
        contentView.addSubview(subTile)
        subTile.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview().inset(10)
            make.left.right.equalToSuperview().inset(18)
        }

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



class BTESelectAlertViewController: BTEAlertViewController,UITableViewDelegate,UITableViewDataSource {

    
    let backgroundImageView = UIImageView(image: UIImage(named: "Group efweew1171277256"))
    
    private let titleLabel = UILabel()
    
    let lists = BTEBaseTableViewController()
    
    var choise:(()->())?
    
    var m:[BTECarnepalaepanderismaciologyNetModel]? {
        didSet {
            guard let model = m else {
                return
            }
            lists.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        mainv.addSubview(backgroundImageView)
        backgroundImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo((UIScreen.main.bounds.width - 34) * backgroundImageView.frame.height / backgroundImageView.frame.width)
        }
        
        titleLabel.textColor = .colorWithHex("#082252")
        titleLabel.font = .systemFont(ofSize: 18, weight: .bold)
        titleLabel.textAlignment = .center
        titleLabel.text = BTELanguageManager.shared.localizedString(forKey: "Please Selecte")
        mainv.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(38)
            make.top.equalToSuperview().offset(44)
        }
        
        mainv.addSubview(lists)
        lists.backgroundColor = .clear
        lists.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(22)
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.bottom.equalToSuperview().offset(-30)
        }
        
        lists.delegate = self
        lists.dataSource = self
        
        lists.register(BTEListItemCell.self, forCellReuseIdentifier: BTEListItemCell.indetification)
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (m ?? []).count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: BTEListItemCell.indetification, for: indexPath) as? BTEListItemCell else {
            return UITableViewCell()
        }
        
        guard let ax = m?[indexPath.row] else {
            return UITableViewCell()
        }
        
        cell.subTile.text = ax.manificum
        
        if ax.choised {
            cell.subTile.textColor = .colorWithHex("#187CA5")
            cell.border.isHidden = false
        } else {
            cell.subTile.textColor = .colorWithHex("#000000")
            cell.border.isHidden = true
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        m?.forEach({$0.choised = false})
        m?[indexPath.row].choised = true
        tableView.reloadData()
    }

//    func findDikegraveValue(in data: BTECarnepalaepanderismaciologyNetModel) -> Int? {
//        return data.panderism.first { $0.manificum == data.tripylaean }?.dikegrave
//    }

}
